<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UsersRender;
use App\Admin\Repositories\WxSearch;
use App\Models\User\WxUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxSearchController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxSearch(['user']), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id','search_content', 'user.user_name'])->placeholder('搜索id，内容，用户名...')->width(35);
            $grid->column('id')->sortable();
            $grid->column('user.user_avatar','头像')->image('',30,30);
            $grid->column('user.user_name',)->display(function ($v) {
                if($v){
                    return '<a target="_blank" href="'.admin_url('users?_search_='.$v).'">'.$v.'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('search_content')->label();
            $grid->column('is_hot')->switch()->sortable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('is_hot')->select([0=>'否',1=>'是']);
            });
//            $grid->disableCreateButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxSearch(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('search_content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxSearch(), function (Form $form) {
            $form->display('id');

            $form->select('user_id', '用户')->options(function ($id) {
                $user = \App\Models\User\WxUser::find($id);
                if ($user) {
                    return [$user->id => $user->user_name];
                }
            })->ajax('select/users');

//            $form->multipleSelectTable('user_id')
//                ->title('用户')
//                ->max(2)
//                ->from(UsersRender::make())
//                ->model(\App\Models\User\WxUser::class, 'id', 'user_name')->saving(function ($v) {
//                    if(is_array($v) && count($v) > 0){
//                        return $v[0];
//                    }else if(is_numeric($v)){
//                        return $v;
//                    }
//                    return 0;
//                })->required();

            $form->text('search_content');
            $form->switch('is_hot');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
